<template>
  <div>
    <el-card>
      <CategorySelector
        @changeCategory="changeCategory"
        :isShowCard="isShowCard"
      ></CategorySelector>
    </el-card>
    <el-card style="margin-top: 20px" v-show="!isShowSku && !isShowSpu">
      <el-button 
        type="primary"
        icon="el-icon-plus"
        :disabled="!category3Id"
        @click="showAddSpuForm(category3Id)"
        >添加SPU</el-button
      >
      <el-table border :data="spuList" style="width: 100%">
        <el-table-column
          type="index"
          width="80"
          label="序号"
          align="center"
        ></el-table-column>

        <el-table-column label="SPU名称" width="width" prop="spuName">
        </el-table-column>

        <el-table-column label="SPU描述" width="width" prop="description">
        </el-table-column>

        <el-table-column label="操作" width="width">
          <template slot-scope="{ row, $index }">
            <el-tooltip
              class="item"
              effect="dark"
              content="添加SKU"
              placement="top-end"
              @click ="showAddSkuFrom(row)"
            >
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              content="修改SPU"
              placement="top"
            >
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click='alterSpuFrom(row)'
              ></el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              content="查看SPU列表"
              placement="top-start"
            >
              <el-button
                type="info"
                icon="el-icon-warning"
                size="mini"
              ></el-button>
            </el-tooltip>

            <el-popconfirm
              style="margin-left: 10px"
              confirm-button-text="确认"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              :title="`确认删除:${row.spuName}吗？`"
              @onConfirm="deletespu(row)"
            >
              <el-tooltip
                class="item"
                effect="dark"
                content="删除SPU列表"
                placement="top-start"
                slot="reference"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                ></el-button>
              </el-tooltip>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="text-align: center"
        :current-page="page"
        :page-sizes="[3, 5, 7, 10]"
        :page-size="limit"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-card style="margin-top: 20px" v-show="isShowSku" >
      <skuForm></skuForm>
    </el-card>
    
    <el-card style="margin-top: 20px"  v-show="isShowSpu">
      <spuForm 
        ref="spu"
        :visible.sync="isShowSpu"
        @saveSpu='saveSpu'
      >
      </spuForm>
    </el-card>
    
  </div>
</template>

<script>
import skuForm from "@/views/product/components/SkuForm.vue";
import spuForm from "@/views/product/components/SpuForm.vue";
export default {
  name: "spu",
  components: {
    skuForm,
    spuForm,
  },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      spuList: [],
      page: 1,
      limit: 3,
      isShowCard: true,
      total: 0,

      isShowSku: false,
      isShowSpu: false,
    };
  },
  methods: {
    //根据3ID获取三级列表
    changeCategory({ categoryId, lveal }) {
      if (lveal === 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
        this.spuList = [];
      } else if (lveal === 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
        this.spuList = [];
      } else {
        this.category3Id = categoryId;
        this.page = 1;
        this.getSpuList();
      }
    },
    //获取SPU列表的函数
    async getSpuList() {
      let { page, limit, category3Id } = this;
      try {
        const result = await this.$API.spu.getList(page, limit, category3Id);
        if (result.code === 200) {
          this.spuList = result.data.records;
          this.total = result.data.total;
        }
      } catch (error) {
        this.$message.error("网络连接超时，请刷新页面");
      }
    },
    handleSizeChange(size) {
      this.limit = size;
      this.getSpuList();
    },
    handleCurrentChange(size) {
      this.page = size;
      this.getSpuList();
    },
    //点击添加SPU按钮
    showAddSpuForm(category3Id){
      this.isShowSpu = true;
      this.$refs.spu.initAddSpuFromData(category3Id);
    },
    //点击修改SPU按钮
    alterSpuFrom(row){
      this.isShowSpu = true;
      this.$refs.spu.initUpdataSpuFormData(row);
    },
    //点击添加SKU按钮
    showAddSkuFrom(row){
      this.isShowSku = true;
      console.log(row);
    },
    //保存SPU的函数
    saveSpu(flag){
      //此处传值代表修改，不传则为添加
      if(flag){
        this.getSpuList();
      }else{
        this.page = 1;
        this.getSpuList();
      }
    },
    //删除SPU的函数
    async deletespu(row){
      try {
        await this.$API.spu.remove(row.id);
              this.$message.success(`已删除:${row.spuName}`)
              if(this.spuList.length === 1){
                this.page > 1 ? this.page -= 1:this.page
              }
              this.getSpuList();

      } catch (error) {
        this.$message.error(error.message)
      }
      
    }
  },
};
</script>

<style lang="scss" scoped>
</style>